// 用于列表的渲染
<template>
  <div>
    <el-table :data="bannerList" border style="width: 100%">
      <el-table-column prop="id" label="轮播图编号" width="180">
      </el-table-column>
      <el-table-column prop="title" label="轮播图标题" width="180">
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="item">
          <div>
            <img class="img" :src="
              item.row.img
                ? $imgUrl + item.row.img
                : 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1870521716,857441283&fm=26&gp=0.jpg'
            " alt="" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <!-- tag标签 -->
          <div>
            <el-tag v-if="item.row.status == 1" type="success">启 用</el-tag>
            <el-tag v-else type="danger">禁 用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" size="small"  round @click="edit(item.row.id)"
              >编辑</el-button
            >
            <!-- {{item.row}}    代表要删除的每一条数据 -->
            <!-- 可以根据指定的 菜单编号(id) 进行删除-->
            <el-button type="danger" size="small"  round @click="del(item.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
// 引入接口文档
import { getBannerDelete } from "../../util/axios";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      bannerList: "banner/getBannerList"
    })
  },
  mounted() {
    //当列表渲染触发行动
    this.getBannerListAction();
  },
  methods: {
    ...mapActions({
      getBannerListAction: "banner/getBannerListAction"
    }),
    // 删除按钮事件
    del(id) {
      this.$confirm("请确定是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 调取删除接口
          getBannerDelete({
            id
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg);
              //当列表删除成功触发行动（刷新页面）
              this.getBannerListAction();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 封装一个编辑事件
    edit(id) {
      // 告诉父组件，要打开编辑弹框顺便传一个id
      this.$emit("edit", id);
    }
  }
};
</script>

<style lang="" scoped>
.img{
  width: 120px;
  height: 120px;
}
</style>
